package com.atguigu.day31.cookie03;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

/*
Cookie: 客户端的会话技术  服务器将数据保留到客户端
     基本使用:
          创建Cookie
          添加到响应对象中
          请求对象 获取Cookie
     设置:携带cookie的路径
         设置Cookie的失效时间
     流程:
        1.客户端向服务器发起请求 服务器创建Cookie 添加到响应对象
        2.客户端下一次请求时会携带Cookie




 */
@WebServlet("/cookieA")
public class CookieServletA extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        System.out.println("CookieServletA 请求到达了");

        Cookie a = new Cookie("username","libai");
        Cookie b = new Cookie("age","20");

        for (int i = 0; i < 10; i++) {
            Cookie c1 = new Cookie("haha"+i,"anqila"+i);
            resp.addCookie(c1);
        }
        //设置携带cookie的路径
        a.setPath("/day31WEB_Web_exploded/cookieB");//username
        b.setPath("/day31WEB_Web_exploded/cookieC");//age

        //设置失效时间 可以持久保留
        b.setMaxAge(60);//秒

        resp.addCookie(a);
        resp.addCookie(b);





        resp.getWriter().println("CookieServletA Success");


    }
}
